package menu

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PageListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PageListLogic {
	return &PageListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PageListLogic) PageList(req *types.MenuPageListReq) (resp *types.MenuPageListResp, err error) {
	// 1.builder rpc request
	pageListReq := pb.MenuPageListReq{PageNum: int32(req.PageNum),
		PageSize: int32(req.PageSize), Perms: req.Perms, Url: req.Url,
		MenuType: int32(req.MenuType)}
	// 2.request rpc
	menuPageList, err := l.svcCtx.UserCenterClient.MenuPageList(l.ctx, &pageListReq)
	if err != nil {
		return nil, err
	}
	// 3.return obj
	resp = new(types.MenuPageListResp)
	resp.TotalPage = int(menuPageList.TotalPage)
	resp.CurrentPage = int(menuPageList.CurrentPage)
	if resp.TotalPage <= 0 {
		return resp, nil
	}
	roleInfoDataList := make([]types.MenuInfoResp, 0)
	dataList := menuPageList.GetData()
	for _, v := range dataList {
		roleInfoDataList = append(roleInfoDataList, types.MenuInfoResp{
			MenuId:   v.MenuId,
			ParentId: v.ParentId,
			Name:     v.Name,
			Url:      v.Url,
			Perms:    v.Perms,
			MenuType: int(v.MenuType),
			Icon:     v.Icon,
			OrderNum: int(v.OrderNum),
		})
	}
	resp.Data = roleInfoDataList
	return
}
